﻿using SD.Toolkits.Mapper;
using SlamDunk.MES.Domain.Entities.AcquisitionContext;
using SlamDunk.MES.IAppService.DTOs.Outputs.AcquisitionContext;
using SlamDunk.MES.IAppService.DTOs.Outputs.DefinitionContext;
using SlamDunk.MES.IAppService.DTOs.Outputs.ExecutionContext;
using SlamDunk.MES.IAppService.DTOs.Outputs.PlanContext;
using SlamDunk.MES.IAppService.DTOs.Outputs.ResourceContext;
using System;
using System.Collections.Generic;

namespace SlamDunk.MES.AppService.Maps
{
    /// <summary>
    /// 数据采集相关映射
    /// </summary>
    public static class AcquisitionMap
    {
        #region # 采集规则映射 —— static AcquisitionRuleInfo ToDTO(this AcquisitionRule rule...
        /// <summary>
        /// 采集规则映射
        /// </summary>
        public static AcquisitionRuleInfo ToDTO(this AcquisitionRule rule, IDictionary<Guid, ProcessInfo> processInfos, IDictionary<Guid, ProcessSegmentInfo> processSegmentInfos)
        {
            AcquisitionRuleInfo ruleInfo = rule.Map<AcquisitionRule, AcquisitionRuleInfo>();
            ruleInfo.ProcessInfo = rule.ProcessId.HasValue
                ? processInfos?[rule.ProcessId.Value]
                : null;
            ruleInfo.ProcessSegmentInfo = rule.ProcessSegmentId.HasValue
                ? processSegmentInfos?[rule.ProcessSegmentId.Value]
                : null;

            return ruleInfo;
        }
        #endregion

        #region # 采集规则项映射 —— static AcquisitionRuleItemInfo ToDTO(this AcquisitionRuleItem ruleItem...
        /// <summary>
        /// 采集规则项映射
        /// </summary>
        public static AcquisitionRuleItemInfo ToDTO(this AcquisitionRuleItem ruleItem, IDictionary<string, EquipmentInfo> equipmentInfos)
        {
            AcquisitionRuleItemInfo ruleItemInfo = ruleItem.Map<AcquisitionRuleItem, AcquisitionRuleItemInfo>();
            ruleItemInfo.EquipmentInfo = string.IsNullOrWhiteSpace(ruleItem.EquipmentNo)
                ? null
                : equipmentInfos?[ruleItem.EquipmentNo];

            return ruleItemInfo;
        }
        #endregion

        #region # 采集会话映射 —— static AcquisitionSessionInfo ToDTO(this AcquisitionSession session...
        /// <summary>
        /// 采集会话映射
        /// </summary>
        public static AcquisitionSessionInfo ToDTO(this AcquisitionSession session, IDictionary<Guid, ProcessInfo> processInfos, IDictionary<Guid, ProcessSegmentInfo> processSegmentInfos, IDictionary<Guid, ExecutionPlanInfo> executionPlanInfos, IDictionary<Guid, WorkOrderInfo> workOrderInfos)
        {
            AcquisitionSessionInfo sessionInfo = session.Map<AcquisitionSession, AcquisitionSessionInfo>();
            sessionInfo.ProcessInfo = session.ProcessId.HasValue
                ? processInfos?[session.ProcessId.Value]
                : null;
            sessionInfo.ProcessSegmentInfo = session.ProcessSegmentId.HasValue
                ? processSegmentInfos?[session.ProcessSegmentId.Value]
                : null;
            sessionInfo.ExecutionPlanInfo = session.ExecutionPlanId.HasValue
                ? executionPlanInfos?[session.ExecutionPlanId.Value]
                : null;
            sessionInfo.WorkOrderInfo = session.WorkOrderId.HasValue
                ? workOrderInfos?[session.WorkOrderId.Value]
                : null;

            return sessionInfo;
        }
        #endregion

        #region # 采集记录映射 —— static AcquisitionRecordInfo ToDTO(this AcquisitionRecord record...
        /// <summary>
        /// 采集记录映射
        /// </summary>
        public static AcquisitionRecordInfo ToDTO(this AcquisitionRecord record, IDictionary<string, EquipmentInfo> equipmentInfos)
        {
            AcquisitionRecordInfo recordInfo = record.Map<AcquisitionRecord, AcquisitionRecordInfo>();
            recordInfo.EquipmentInfo = string.IsNullOrWhiteSpace(record.EquipmentNo)
                ? null
                : equipmentInfos?[record.EquipmentNo];

            return recordInfo;
        }
        #endregion
    }
}
